package com.l024.easyandroidlib.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import com.hjq.toast.ToastUtils;
import com.l024.easyandroidlib.dialog.manager.DialogManager;
import com.l024.easyandroidlib.dialog.view.LoadingDialog;

/**
 * @Author: Cat
 * @CreateTime: 2022-07-29  17:01
 * @Description: TODO
 * @Version: 1.0
 */
public abstract class BaseFragment <VDB extends ViewDataBinding> extends Fragment {
    protected VDB mBinding;
    private LoadingDialog loadingDialog;
    //解决弹窗问题
    private Handler mHandler = new Handler();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater,getLayoutId(),container,false);
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        initEvent();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hideLoading();
    }

    // 获取布局
    public abstract int getLayoutId();

    public abstract void initView();

    public abstract void initData();

    public abstract void initEvent();

    /**
     * 直接跳转Activity
     * @param cls
     */
    public void go(Class cls){
        Intent intent = new Intent(getActivity(),cls);
        getActivity().startActivity(intent);
    }

    /**
     * 携带数据跳转Activity
     * @param cls
     */
    public void go(Class cls,Bundle bundle){
        Intent intent = new Intent(getActivity(),cls);
        intent.putExtras(bundle);
        getActivity().startActivity(intent);
    }

    /**
     * 携带返回值数据跳转Activity
     * @param cls
     */
    public void go(Class cls, int resultCode){
        Intent intent = new Intent(getActivity(),cls);
        getActivity().startActivityForResult(intent,resultCode);
    }

    /**
     * 携带返回值数据跳转Activity
     * @param cls
     */
    public void go(Class cls,Bundle bundle,int resultCode){
        Intent intent = new Intent(getActivity(),cls);

        intent.putExtras(bundle);
        getActivity().startActivityForResult(intent,resultCode);
    }


    /**
     * 封装toast方法（自行去实现）
     *
     * @param str
     */
    public void showToast(String str) {
        ToastUtils.show(str);
    }

    public void showLoading(){
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity());
            DialogManager.getInstance().show(loadingDialog);
        }else {
            mHandler.removeCallbacks(runnable);
        }
    }
    public void hideLoading(){
        mHandler.postDelayed(runnable, 200);
    }

    //解决网络加载框问题
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (loadingDialog != null && loadingDialog.isShowing()) {
                DialogManager.getInstance().hide(loadingDialog);
                loadingDialog = null;
            }
        }
    };
}
